
import util from "../../common/js/util.js";
var app = getApp();

Component({
  mixins: [],
  data: {
    showAreaBottom: false,
    areaName: "",
    provinceList: [],
    cityList: [],
    countyList: [],
    provinceIndex: 0,
    cityIndex: 0,
    countyIndex: 0,
    submitResult: {
      areaCnNames: "",
      areaIdNames: ""
    },
    valueArr: []
  },
  props: {
    key_name: "",
    onAffirmFn: function onAffirmFn(e) { }
  },
  didMount() {
    this.provinceListFn();
  },
  didUpdate() { },
  didUnmount() { },
  methods: {
    areaPickerChange(e) {
      let areaIndex = e.detail.value;
      let name = this.data.areaList[areaIndex].AREANAME;
    //  console.log(name);

    },
    changeFormList(item) {
      let formList = [];
      switch (item) {
        case "专业行政": case "同关行业": case "同业相关":
          formList = param.resource;
          break;
        case "材料商":
          formList = param.resourceMaterials;
          break;
        case "机车":
          formList = param.resourceMotorcycle;
          break;
        case "班组": case "人工班组":
          formList = param.resourceTeamOrGroup;
          // searchList.CONSTRUCTION = [];
          break;
        case "工头":
          formList = param.resourceGanger;
          break;
        case "后勤事务":
          formList = param.resourceLogistics;
          break;
      };
      console.log(formList);

      this.setData({
        formList, 'searchList.CONSTRUCTION': []
      })
    },
    bindKeyInput(e) {
      let key = e.currentTarget.dataset.key;
      util.debounce(() => {
        this.setData({
          [`${key}`]: e.detail.value,
        });
      }, 300);

    },
    openAreaFn(e) {
      this.setData({
        showAreaBottom: true,
        valueArr: [0,0,0]
      })
      // console.log(this.data.showAreaBottom);

    },
    closeAreaFn() {
      this.setData({
        showAreaBottom: false
      })
    },
    onChange(e) {

      let valueArr = e.detail.value;
      console.log(valueArr);
      let provinceIndex = valueArr[0], cityIndex = "", countyIndex = "";
      // debugger
      if (valueArr.length > 0) {
        let c_PARENTID = this.data.areaList[provinceIndex].AREAID;
        cityIndex = valueArr[1];
        this.cityListFn(c_PARENTID);
      }
      // if (valueArr.length > 1) {
      //   let c_PARENTID = this.data.cityList[cityIndex].AREAID;
      //   countyIndex = valueArr[2];
      //   this.countyListFn(c_PARENTID)
      // }
      this.setData({
        provinceIndex, cityIndex, countyIndex, valueArr
      })
    },
    provinceListFn(name) {
      let data = { PARENTID: -1 }
      app.$post("/YhERP/area/json/listAllArea", data).then(res => {
        if (res.is == 1) {
          let areaList = res.data;
          this.setData({
            areaList
          })
          let c_PARENTID = this.data.areaList[0].AREAID;
          this.cityListFn(c_PARENTID, 1);
        }
      });
    },
    cityListFn(PARENTID, init) {
      let data = { PARENTID }
      app.$post("/YhERP/area/json/listAllArea", data).then(res => {
        if (res.is == 1) {
          let cityList = res.data;
          this.setData({
            cityList
          })
          if (util.isNotEmpty(init) || res.data.length == 1) {
            let c_PARENTID = cityList[0].AREAID;
            this.countyListFn(c_PARENTID)
          } else if (this.data.valueArr.length > 1) {
            let cityIndex = this.data.valueArr[1];
            let c_PARENTID = this.data.cityList[cityIndex].AREAID;
            this.countyListFn(c_PARENTID)
          }

        }
      });
    },
    countyListFn(PARENTID) {
      let data = { PARENTID }
      app.$post("/YhERP/area/json/listAllArea", data).then(res => {
        if (res.is == 1) {
          let countyList = res.data;
          this.setData({
            countyList
          })
        }
      });
    },
    getSumitData() {
      let submitResult = {
        areaCnNames: "",
        areaIdNames: ""
      }
      // AREAID AREANAME
      let province = this.data.areaList[this.data.valueArr[0]];
      let city = this.data.cityList[this.data.valueArr[1]];
      let count = this.data.countyList[this.data.valueArr[2]];
      let areaCnArr = [], areaIdArr = [];
      areaCnArr.push(province.AREANAME);
      areaCnArr.push(city.AREANAME);
      areaCnArr.push(count.AREANAME);
      areaIdArr.push(province.AREAID);
      areaIdArr.push(city.AREAID);
      areaIdArr.push(count.AREAID);
      submitResult.areaCnNames = areaCnArr.join(",");
      submitResult.areaIdNames = areaIdArr.join(",");
      submitResult.key_name=this.props.key_name;
     // console.log(submitResult);
      return submitResult;
    },
    submit() {
      if (this.data.valueArr.length > 3) {
        util.message("请选择到县/区!");
        return false;
      }
      let data=this.getSumitData();
      this.props.onAffirmFn(data);
      this.cancel();
    },
    cancel() {
      this.setData({
        valueArr: [0,0,0],
        cityList: [],
        areaList: [],
        countyList: []
      })
      this.provinceListFn()
      this.closeAreaFn();
    }
  },
});
